//
//  UINavigationController+hideBottomBar.m
//  基本结构
//
//  Created by 蓝其 on 16/10/19.
//  Copyright © 2016年 蓝其. All rights reserved.
//
#import "UINavigationController+RBAdd.h"
#import "NSObject+YYAdd.h"

static char *const RB_AUTO_HIDE_BOTTOM_BAR_KEY = "RB_AUTO_HIDE_BOTTOM_BAR_KEY";

@implementation UIViewController(RBHideBottomBar)

- (void)setDisableAutoHideBottomBar:(BOOL)autoHideBottomBar
{
    [self setAssociateValue:[NSNumber numberWithBool:autoHideBottomBar] withKey:RB_AUTO_HIDE_BOTTOM_BAR_KEY];
}

- (BOOL)disableAutoHideBottomBar
{
    return [[self getAssociatedValueForKey:RB_AUTO_HIDE_BOTTOM_BAR_KEY] boolValue];
}

@end

@implementation UINavigationController(RBAdd)

+ (void)load
{
    [self swizzleInstanceMethod:@selector(pushViewController:animated:) with:@selector(swizzledPushViewController:animated:)];
}

- (void)swizzledPushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.viewControllers.count == 1)
    {
        UIViewController *vc = self.viewControllers.lastObject;
        if (!vc.disableAutoHideBottomBar)
        {
            viewController.hidesBottomBarWhenPushed = YES;
        }
    }
    [self swizzledPushViewController:viewController animated:animated];
}

- (void)removeViewControllers: (NSArray *)vcs animated: (BOOL)animated
{
    for (UIViewController *subVc in vcs)
    {
        [subVc.presentedViewController dismissViewControllerAnimated:NO completion:nil];
    }
    NSMutableArray *arrayM = [NSMutableArray arrayWithArray:self.viewControllers];
    [arrayM removeObjectsInArray:vcs];
    [self setViewControllers:arrayM animated:animated];
}

- (void)pushToViewController: (UIViewController *)vc animated: (BOOL)animated removeViewControllers: (NSArray *)vcs;
{
    [self pushViewController:vc animated:animated];
    [self removeViewControllers:vcs animated:NO];
}

- (void)pushToViewController: (UIViewController *)vc animated: (BOOL)animated removeVcCount: (NSInteger)vcCount;
{
    NSMutableArray *vcsM = NSMutableArray.array;
    if(vcCount)
    {
        for(NSInteger i = self.viewControllers.count - 1; i > self.viewControllers.count - vcCount - 1; i--)
        {
            [vcsM addObject:self.viewControllers[i]];
        }
    }
    [self pushViewController:vc animated:animated];
    if(vcsM.count)
    {
        [self removeViewControllers:vcsM animated:NO];
    }
}

- (void)popToViewControllerWithClass: (Class)vcClass animated: (BOOL)animated
{
    for (UIViewController *subVc in self.viewControllers)
    {
        if ([subVc isKindOfClass:vcClass])
        {
            [self popToViewController:subVc animated:YES];
            break;
        }
    }
}

- (void)popToViewControllerWithIndex: (NSInteger)index animated: (BOOL)animated
{
    if (index < self.viewControllers.count)
    {
        [self popToViewController:self.viewControllers[index] animated:animated];
    }
}

- (void)popViewControllerWithCount:(NSInteger)vcCount animated:(BOOL)animated
{
    if (vcCount < self.viewControllers.count)
    {
        [self popToViewController:self.viewControllers[self.viewControllers.count - vcCount - 1] animated:animated];
    }
}

@end
